package com.zhijiang.hospital.service.impl;

import com.zhijiang.hospital.mapper.RoleMapper;
import com.zhijiang.hospital.mapper.SysRoleMenuMapper;
import com.zhijiang.hospital.mapper.SysUserRoleMapper;
import com.zhijiang.hospital.mapper.UserMapper;
import com.zhijiang.hospital.service.RoleService;
import com.zhijiang.util.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    RedisCache redisCache;
    @Resource
    RoleMapper roleMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    SysRoleMenuMapper sysRoleMenuMapper;
    @Autowired
    SysUserRoleMapper sysUserRoleMapper;
    //@Override
    //public ResponseResult add(SysRole sysRole, HttpServletRequest request) {
    //
    //    //SysUser sysUser = UserHolder.localUser().getSysUser();
    //    //sysRole.setStatus("1");
    //    //sysRole.setDelFlag("1");
    //    //sysRole.setCreateBy(sysUser.getUsername());
    //    //sysRole.setUpdateBy(sysUser.getUsername());
    //    //roleMapper.insert(sysRole);
    //    return new  ResponseResult(200,"成功");
    //}
    //
    //@Override
    //public ResponseResult allRole() {
    //    List<SysRole> sysRoles = roleMapper.selectList(new LambdaQueryWrapper<SysRole>()
    //           .eq(SysRole::getDelFlag,1).eq(SysRole::getStatus,1));
    //    return new ResponseResult(200,sysRoles);
    //}
    //
    //@Override
    //public ResponseResult delete(SysRole sysRole) {
    //    SysRole role = roleMapper.selectOne(new LambdaQueryWrapper<SysRole>()
    //            .eq(SysRole::getRoleid,sysRole.getRoleid()));
    //    if (Objects.isNull(role)){
    //        throw new RuntimeException("角色不存在");
    //    }
    //    if (role.getStatus().equals(1)){
    //        throw new RuntimeException("角色未停用");
    //    }
    //    if (role.getDelFlag().equals(0)){
    //        throw new RuntimeException("用户已删除");
    //    }
    //    sysRole.setDelFlag("0");
    //    roleMapper.updateById(sysRole);
    //    return new ResponseResult();
    //}
    //
    //@Override
    //public ResponseResult stop(SysRole sysRole) {
    //    SysRole role = roleMapper.selectOne(new LambdaQueryWrapper<SysRole>()
    //            .eq(SysRole::getRoleid,sysRole.getRoleid()));
    //    if (Objects.isNull(role)){
    //        throw new RuntimeException("角色不存在");
    //    }
    //    SysRoleMenu sysRoleMenu = sysRoleMenuMapper.selectOne(new LambdaQueryWrapper<SysRoleMenu>()
    //            .eq(SysRoleMenu::getRoleid, sysRole.getRoleid()));
    //    SysUserRole sysUserRole = sysUserRoleMapper.selectOne(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getRoleid, role.getRoleid()));
    //    if (!Objects.isNull(sysRoleMenu)||!Objects.isNull(sysUserRole)){
    //        throw new RuntimeException("此角色已产生关联请先解除关联");
    //    }
    //
    //    if (role.getStatus().equals(0)||role.getDelFlag().equals(0)){
    //        throw new RuntimeException("角色已停用或者已删除");
    //    }
    //    sysRole.setStatus("0");
    //    roleMapper.updateById(sysRole);
    //    return new ResponseResult(200,"成功") ;
    //}
    //
    //@Override
    //public ResponseResult relevancy(RoleDto roleDto) {
    //    SysRole sysRole = roleMapper.selectById(roleDto.getRoleid());
    //    SysUser sysUser = userMapper.selectById(roleDto.getUserid());
    //    if (Objects.isNull(sysRole)|| Objects.isNull(sysUser)){
    //        throw new RuntimeException("角色或者用户不存在");
    //    }
    //    if (!sysRole.getStatus().equals("1")||
    //            !sysRole.getDelFlag().equals("1")||
    //            !sysUser.getStatus().equals("1")){
    //        throw new RuntimeException("角色或者用户不存在");
    //    }
    //    SysUserRole sysUserRoles = sysUserRoleMapper
    //            .selectOne(new LambdaQueryWrapper<SysUserRole>()
    //                    .eq(SysUserRole::getRoleid, roleDto.getRoleid())
    //                    .eq(SysUserRole::getUserid, roleDto.getUserid()));
    //    if (!Objects.isNull(sysUserRoles)){
    //        throw new RuntimeException("该关联已存在");
    //    }
    //    SysUserRole sysUserRole = new SysUserRole();
    //    BeanUtils.copyProperties(roleDto,sysUserRole);
    //
    //    sysUserRole.setCode(IdWorker.getIdStr());
    //
    //    roleMapper.add(sysUserRole);
    //
    //
    //    return new ResponseResult(200,"成功");
    //}
    //
    //@Override
    //public ResponseResult reliveRole(RoleDto roleDto) {
    //    SysUserRole sysUserRole = sysUserRoleMapper
    //            .selectOne(new LambdaQueryWrapper<SysUserRole>()
    //                    .eq(SysUserRole::getRoleid, roleDto.getRoleid())
    //                    .eq(SysUserRole::getUserid, roleDto.getUserid()));
    //
    //    if (Objects.isNull(sysUserRole)){
    //        throw new RuntimeException("此权限或者用户不存在");
    //    }
    //    roleMapper.deleteRole(roleDto);
    //    return new  ResponseResult(200,"删除成功");
    //}


}
